package cn.iocoder.yudao.module.member.service.majorobtain;

import cn.iocoder.yudao.module.member.dal.dataobject.major.MajorDO;
import cn.iocoder.yudao.module.member.dal.mysql.major.MajorMapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import cn.iocoder.yudao.module.member.controller.admin.majorobtain.vo.*;
import cn.iocoder.yudao.module.member.dal.dataobject.majorobtain.MajorObtainDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import cn.iocoder.yudao.module.member.dal.mysql.majorobtain.MajorObtainMapper;

import javax.annotation.Resource;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.member.enums.ErrorCodeConstants.*;

/**
 * 专业表-就业方向 Service 实现类
 *
 * @author Alan
 */
@Service
@Validated
public class MajorObtainServiceImpl implements MajorObtainService {

    @Resource
    private MajorObtainMapper majorObtainMapper;

    @Resource
    private MajorMapper majorMapper;

    @Override
    public Long createMajorObtain(MajorObtainSaveReqVO createReqVO) {
        // 插入
        MajorObtainDO majorObtain = BeanUtils.toBean(createReqVO, MajorObtainDO.class);
        majorObtainMapper.insert(majorObtain);
        // 返回
        return majorObtain.getId();
    }

    @Override
    public void updateMajorObtain(MajorObtainSaveReqVO updateReqVO) {
        // 校验存在
        validateMajorObtainExists(updateReqVO.getId());
        // 更新
        MajorObtainDO updateObj = BeanUtils.toBean(updateReqVO, MajorObtainDO.class);
        majorObtainMapper.updateById(updateObj);
    }

    @Override
    public void deleteMajorObtain(Long id) {
        // 校验存在
        validateMajorObtainExists(id);
        // 删除
        majorObtainMapper.deleteById(id);
    }

    private void validateMajorObtainExists(Long id) {
        if (majorObtainMapper.selectById(id) == null) {
            throw exception(MAJOR_OBTAIN_NOT_EXISTS);
        }
    }

    @Override
    public MajorObtainDO getMajorObtain(Long id) {
        return majorObtainMapper.selectById(id);
    }

    @Override
    public List<MajorObtainDO> getMajorObtainDO(Long majorId) {
        return majorObtainMapper.selectList(new LambdaQueryWrapper<MajorObtainDO>().eq(MajorObtainDO::getMajorId,majorId));
    }

    @Override
    public PageResult<MajorObtainDO> getMajorObtainPage(MajorObtainPageReqVO pageReqVO) {
        return majorObtainMapper.selectPage(pageReqVO);
    }

    @Override
    public void importExcel(List<MajorObtainRespVO> list) {
        List<MajorObtainDO> majorObtainDOS =new ArrayList<>();
        List<MajorDO> majorDOS = majorMapper.selectList();
        for (MajorObtainRespVO respVO:list) {
            MajorObtainDO obtainDO =new MajorObtainDO();
            BeanUtils.copyProperties(respVO,obtainDO);
            MajorDO majorDO = majorDOS.stream().filter(v1 -> v1.getName().equals(respVO.getMajorName())).findFirst().orElse(null);
            if (Objects.nonNull(majorDO)) {
                obtainDO.setMajorId(majorDO.getId());
            }
            majorObtainDOS.add(obtainDO);
        }
        majorObtainMapper.insertBatch(majorObtainDOS);
    }
}